using Microsoft.EntityFrameworkCore;
using MusicLibrary.Server.Models;

namespace MusicLibrary.Server.Data
{
    public class MusicLibraryContext : DbContext
    {
        public MusicLibraryContext(DbContextOptions<MusicLibraryContext> options) : base(options) { }

        public DbSet<User> Users { get; set; }
        public DbSet<Song> Songs { get; set; }
        public DbSet<Playlist> Playlists { get; set; }
        public DbSet<UserFavorite> UserFavorites { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置UserFavorite的复合唯一键
            modelBuilder.Entity<UserFavorite>()
                .HasIndex(uf => new { uf.UserId, uf.SongId })
                .IsUnique();

            // 配置Playlist和Song的多对多关系
            modelBuilder.Entity<Playlist>()
                .HasMany(p => p.Songs)
                .WithMany()
                .UsingEntity(j => j.ToTable("PlaylistSongs"));
        }
    }
}